const router = require('koa-router')()

router.get('/getpote',async(ctx,next)=>{
    let sql = 'select * from chinesepoetry'
    let res = await ctx.db.query(sql)

    ctx.body = {'code':200,'msg':res}
  
  })

//更新数据
router.post('/updatapote',async(ctx,next)=>{
    let row = ctx.request.body
    row.edit = false
    let sql = 'update chinesepoetry set name=?,content=?,author=?,dynasty=?,shangxi=?,fanyi=? where poetryId=?'
    await ctx.db.query(sql,[row.name,row.content,row.author,row.dynasty,row.shangxi,row.fanyi,row.poetryId])
  
    ctx.body={'code':200,"msg":"编辑成功"}
  
  })
  //搜索
  router.post('/searchpoet',async(ctx,next)=>{
    let key = ctx.request.body.key
    console.log(key);
    let sql = "select * from chinesepoetry where name like concat('%',?,'%') || author like concat(?,'%')"
    let res = await ctx.db.query(sql,[key,key])
    ctx.body = {'code':200,'msg':res}
  })


  //删除shici数据
router.post('/deletepote',async(ctx,next)=>{
    let id = ctx.request.body.poetryId
    let sql='delete from chinesepoetry where poetryId=?'
    await ctx.db.query(sql,[id])
    ctx.body={"code":200,"msg":'删除成功'}
  })
  
//添加用户数据
router.post('/addpote', async(ctx,next)=>{
    let row = ctx.request.body
    console.log(row);
    let sql = 'insert into chinesepoetry(name,content,author,dynasty,shangxi,fanyi) values(?,?,?,?,?,?)'
    await ctx.db.query(sql,[row.name,row.content,row.author,row.dynasty,row.shangxi,row.fanyi])
    ctx.body={'code':200,'msg':"添加成功"}
  
  })


  module.exports = router
